package Com.Ring.TradingMobile;

import Com.Ring.TradingMobile.Lib.Common;
import Com.Ring.TradingMobile.Object.Config;
import Com.Ring.TradingMobile.SQLite.DBManager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SettingActivity extends Activity {

	private final static String TAG = "SettingActivityDebug";
	private ToggleButton btnIsRealTime;
	private ToggleButton btnIsAutoSave;
	private SeekBar seekDaysClear;
	private SeekBar seekInterval;
	private Button btnSave;
	private TextView lblDaysClear;
	private TextView lblInterval;
	
	private Button btnBack;

	private int valueDaysClear;
	private int valueInterval;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		FindViewId();
		SetOnListener();

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
		Thread myThread = new Thread(new GetData());
		myThread.start();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	private void FindViewId() {
		btnIsRealTime = (ToggleButton) findViewById(R.id.btnRealTime);
		btnIsAutoSave = (ToggleButton) findViewById(R.id.btnAutoSave);
		seekDaysClear = (SeekBar) findViewById(R.id.seekDaysClear);
		seekInterval = (SeekBar) findViewById(R.id.seekInterval);
		btnSave = (Button) findViewById(R.id.btnSave);
		lblDaysClear = (TextView) findViewById(R.id.lblDaysClear);
		lblInterval = (TextView) findViewById(R.id.lblInterval);
		
		btnBack = (Button)findViewById(R.id.btnBack);
	}

	private void SetOnListener() {
		btnSave.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				DBManager dbData = new DBManager(SettingActivity.this);
				dbData.SaveConfig(seekInterval.getProgress()>0?seekInterval.getProgress():1, btnIsRealTime.isChecked()?1:0, btnIsAutoSave.isChecked()?1:0, seekDaysClear.getProgress()>0?seekDaysClear.getProgress():1);
				dbData.close();
			}
		});

		//

		seekDaysClear.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if(progress>0)
					lblDaysClear.setText(progress + " day");
				else {
					lblDaysClear.setText("1 day");
				}
			}
		});

		//

		seekInterval.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {

			}

			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if(progress!=0)
				lblInterval.setText(progress + " s");
				else {
					lblInterval.setText("1 s");
				}
			}
		});

		btnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
	}

	private void doWork() {
		runOnUiThread(new Runnable() {
			public void run() {
				try {
					DBManager dbData = new DBManager(SettingActivity.this);
					Cursor cur = dbData.Select("select * from "
							+ Config.TABLE_NAME);
					if (cur.getCount() > 0) {
						while (cur.moveToNext()) {
							btnIsRealTime.setChecked(cur.getInt(cur.getColumnIndex(Config.IS_REALTIME))==1?true:false);
							btnIsAutoSave.setChecked(cur.getInt(cur.getColumnIndex(Config.AUTO_SAVE))==1?true:false);
							seekDaysClear.setProgress(cur.getInt(cur.getColumnIndex(Config.AUTO_CLEAR_TIME)));
							seekInterval.setProgress(cur.getInt(cur.getColumnIndex(Config.INTERVAL)));
							lblInterval.setText(seekInterval.getProgress() + " s");
							lblDaysClear.setText(seekDaysClear.getProgress() + " day");
						}
					}
					dbData.close();
				} catch (Exception ex) {
					Log.d(TAG, ex.toString());
				}
			}
		});
	}

	class GetData implements Runnable {
		public void run() {
			try {
				doWork();
			} catch (Exception ex) {
				Log.d(TAG, ex.toString());
			}
		}

	}

}
